<template>
  <a-layout>
    <a-layout-content style="height: calc(100vh - 88px); padding: 2px">
      <VxeBasicTable ref="TableRef" v-bind="options">
        <template #toolbar_buttons>
          <ToolBtn
            :filter-call="handleOpenFilter"
            :create-call="handleEdit"
            :delete-call="handleDelete"
            :show-export="false"
            :show-import="false"
            :show-delete="false"
          />
        </template>
        <template #action="{ row }">
          <Action :record="row" @edit="handleEdit" @delete="handleDelete" />
        </template>
      </VxeBasicTable>
    </a-layout-content>
    <ConfigEdit v-model:visible="showEdit" :data="current" @done="reload" />
    <TableFilter
      ref="FilterRef"
      @done="reload"
      v-model:visible="ifSHowFilter"
      :filter-cols="Columns"
    />
  </a-layout>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import { message } from 'ant-design-vue/es';
  import { messageLoading } from 'ele-admin-pro/es';
  import ConfigEdit from './components/config-edit.vue';
  import { list, remove } from '@/api/system/config';
  import type { SysConfig } from '@/api/system/config/model';
  import { useConfig } from './config';

  const {
    TableRef,
    FilterRef,
    Columns,
    TableFilter,
    ToolBtn,
    Action,
    options
  } = useConfig(list);

  // 当前选中数据
  const current = ref<SysConfig>();

  // 是否显示编辑弹窗
  const showEdit = ref(false);

  const ifSHowFilter = ref(false);

  /* 刷新表格 */
  const reload = () => {
    TableRef.value?.commitProxy('query');
  };

  /* 编辑 */
  const handleEdit = (row: SysConfig) => {
    current.value = row;
    showEdit.value = true;
  };
  const handleOpenFilter = () => {
    ifSHowFilter.value = true;
  };

  /* 删除 */
  const handleDelete = (row: SysConfig) => {
    const hide = messageLoading('请求中..', 0);
    remove(row.id)
      .then((msg) => {
        hide();
        message.success(msg);
        reload();
      })
      .catch((e) => {
        hide();
        message.error(e.message);
      });
  };
</script>

<script lang="ts">
  export default {
    name: 'SystemConfig'
  };
</script>
